#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
更新期货配置脚本
将months和active_months合并为active_months列表
"""

import re
from pathlib import Path

def update_config():
    """更新配置文件"""
    config_file = Path("config/futures_config.py")
    
    with open(config_file, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # 定义品种的活跃月份映射
    active_months_mapping = {
        # 上期所品种
        "hc": [1, 5, 10],
        "cu": [1, 2, 3, 4, 5, 6],  # 限制为6个月
        "al": [1, 2, 3, 4, 5, 6],
        "zn": [1, 2, 3, 4, 5, 6],
        "au": [2, 4, 6, 8, 10, 12],
        "ag": [1, 3, 5, 7, 9, 11],
        "ni": [1, 2, 3, 4, 5, 6],
        "sn": [1, 2, 3, 4, 5, 6],
        "pb": [1, 2, 3, 4, 5, 6],
        "wr": [1, 5, 10],
        "bu": [3, 6, 9, 12],
        "ru": [1, 3, 4, 5, 6, 7],  # 限制为6个月
        "sp": [3, 6, 9, 12],
        "ss": [1, 2, 3, 4, 5, 6],
        "fu": [1, 4, 5, 6, 7, 8],  # 限制为6个月
        "ao": [1, 2, 3, 4, 5, 6],
        "br": [1, 3, 4, 5, 6, 7],  # 限制为6个月
        
        # 大商所品种
        "i": [1, 5, 9],
        "j": [1, 5, 9],
        "jm": [1, 5, 9],
        "m": [1, 3, 5, 7, 8, 9],  # 限制为6个月
        "y": [1, 3, 5, 7, 8, 9],  # 限制为6个月
        "a": [1, 3, 5, 7, 9, 11],
        "b": [1, 3, 5, 7, 9, 11],
        "c": [1, 3, 5, 7, 9, 11],
        "cs": [1, 3, 5, 7, 9, 11],
        "l": [1, 3, 5, 7, 9, 11],
        "v": [1, 3, 5, 7, 9, 11],
        "p": [1, 3, 5, 7, 8, 9],  # 限制为6个月
        "pp": [1, 3, 5, 7, 9, 11],
        "fb": [1, 3, 5, 7, 9, 11],
        "bb": [1, 3, 5, 7, 9, 11],
        "jd": [1, 3, 5, 7, 9, 11],
        "lh": [1, 3, 5, 7, 9, 11],
        "rr": [1, 3, 5, 7, 9, 11],
        "eg": [1, 3, 5, 7, 9, 11],
        "eb": [1, 3, 5, 7, 9, 11],
        "pg": [1, 3, 5, 7, 9, 11],
        "lg": [1, 3, 5, 7, 9, 11],
        "bz": [1, 3, 5, 7, 9, 11],
        "PL": [1, 3, 5, 7, 9, 11],
        "PR": [1, 3, 5, 7, 9, 11],
        
        # 郑商所品种
        "MA": [1, 5, 9],
        "TA": [1, 5, 9],
        "CF": [1, 3, 5, 7, 9, 11],
        "SR": [1, 3, 5, 7, 9, 11],
        "RM": [1, 3, 5, 7, 9, 11],
        "OI": [1, 3, 5, 7, 9, 11],
        "FG": [1, 3, 5, 7, 9, 11],
        "WH": [1, 3, 5, 7, 9, 11],
        "PM": [1, 3, 5, 7, 9, 11],
        "RI": [1, 3, 5, 7, 9, 11],
        "LR": [1, 3, 5, 7, 9, 11],
        "JR": [1, 3, 5, 7, 9, 11],
        "SF": [1, 3, 5, 7, 9, 11],
        "SM": [1, 3, 5, 7, 9, 11],
        "RS": [1, 3, 5, 7, 9, 11],
        "CY": [1, 3, 5, 7, 9, 11],
        "AP": [1, 3, 5, 7, 10, 11],  # 苹果特殊月份
        "CJ": [1, 3, 5, 7, 9, 11],
        "UR": [1, 3, 5, 7, 9, 11],
        "SA": [1, 3, 5, 7, 9, 11],
        "PF": [1, 3, 5, 7, 9, 11],
        "PK": [1, 3, 5, 7, 9, 11],
        "SH": [1, 3, 5, 7, 9, 11],
        "PX": [1, 3, 5, 7, 9, 11],
        "ZC": [1, 3, 5, 7, 9, 11],
        
        # 中金所品种
        "IF": [3, 6, 9, 12],
        "IC": [3, 6, 9, 12],
        "IH": [3, 6, 9, 12],
        "IM": [3, 6, 9, 12],
        "T": [3, 6, 9, 12],
        "TF": [3, 6, 9, 12],
        "TL": [3, 6, 9, 12],
        "TS": [3, 6, 9, 12],
        
        # 上海国际能源交易中心
        "sc": [1, 2, 3, 4, 5, 6],
        "lu": [1, 2, 3, 4, 5, 6],
        "nr": [1, 3, 4, 5, 6, 7],  # 限制为6个月
        "bc": [1, 2, 3, 4, 5, 6],
        "ec": [1, 2, 3, 4, 5, 6],
        
        # 广州期货交易所
        "si": [1, 2, 3, 4, 5, 6],
        "lc": [1, 2, 3, 4, 5, 6],
        "ps": [1, 2, 3, 4, 5, 6],
        "ad": [1, 2, 3, 4, 5, 6],
    }
    
    # 替换每个品种的配置
    for symbol, months in active_months_mapping.items():
        # 查找该品种的配置块
        pattern = rf'"{symbol}": \{{[^}}]+\}}'
        match = re.search(pattern, content, re.DOTALL)
        
        if match:
            old_config = match.group(0)
            
            # 替换months和active_months行
            new_config = re.sub(
                r'"months": \[[^\]]+\],.*?"active_months": \d+,',
                f'"active_months": {months},',
                old_config,
                flags=re.DOTALL
            )
            
            content = content.replace(old_config, new_config)
    
    # 写回文件
    with open(config_file, 'w', encoding='utf-8') as f:
        f.write(content)
    
    print("✓ 期货配置更新完成")

if __name__ == "__main__":
    update_config()